package NetWork;

import java.net.InetAddress;
import java.net.SocketException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.util.Arrays;

public class Server {
    public static void main(String[] args) throws Exception {

        DatagramSocket datagramSocket = new DatagramSocket(9090);

        byte[] bytes = new byte[1024 * 64];
        DatagramPacket packet = new DatagramPacket(bytes, bytes.length);

        while (true) {
            //接收数据
            datagramSocket.receive(packet);

            //输出数据
            System.out.println(new String(bytes,0, packet.getLength()));
            System.out.println("来自客户端的IP号: " + packet.getAddress().getHostAddress() + "，来自客户端的端口号: " + packet.getPort());

            //返回响应
            byte[] bytes1 = "服务器已接收到数据啦!!!返回给客户端".getBytes();
            DatagramPacket packet1 = new DatagramPacket(bytes1,bytes1.length,packet.getAddress(),packet.getPort());
            datagramSocket.send(packet1);
            System.out.println("=====================================================");
        }

        /*datagramSocket.close();*/
    }
}
